/**
 * 
 */
package com.qq.spirit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import com.qq.spirit.DinnerTask.DinnerTeamAction;
import com.qq.spirit.inter.AbstractXmlActionAfterRequest;
import com.qq.spirit.inter.IActionAfterHttpRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

/**
 * loyalty 民忠 forceimposecost 强制征收所需金币 imposenum 征收次数 cdflag 征收时间 征收任务
 * 
 * @author beedoorwei
 * 
 */
public class ImposeTask extends TaskUnit
{
	@Override
	public void doTask()
	{
		if (!GameUserInfoUtil.getIsUserLoadSuc())
		{
			TaskManager.addTask(this, 10);
		} else
		{
			Iterator<Integer> ite = GameUserInfoUtil.userIte();
			while (ite.hasNext())
			{
				Integer gid = ite.next();
				TaskManager.addTask(new Impose(gid));
			}
		}
	}

}

/**
 * 具体征收任务
 * 
 * @author Administrator
 * 
 */
class Impose extends TaskUnit
{
	public Impose(int gid)
	{
		super();
		this.gid = gid;
		this.taskDesc = "征收";
	}

	@Override
	public void doTask()
	{
		try
		{
			HttpUtil.doGet(gid, GameConstants.IMPOSE_PRE_URL, new ImposePreTaskAction(gid));
		} catch (Exception e)
		{
			LoggerUtil.error(gid, e);
		}
	}
}

/**
 * 
 * 征收前事件查询
 * 
 * @author Administrator
 * 
 */
class ImposePreTaskAction extends AbstractXmlActionAfterRequest
{
	public ImposePreTaskAction(int gid)
	{
		super();
		this.gid = gid;
		this.taskUnit = new Impose(gid);
	}

	public void doCall(List<Header> headerList, XmlNodeData imposeInfo)
	{
		UserData u = GameUserInfoUtil.getUser(gid);
		// 判断能否征收
		String imposenum = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.imposenum");
		String cdflag = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.cdflag");
		String lastimposetime = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.lastimposetime");
		if (GameUtil.convertInt(imposenum) <= 0)
		{
			return;
		}
		if (cdflag.equals("1") && GameUtil.convertInt(lastimposetime) > 0)
		{
			LoggerUtil.info(gid, GameUserInfoUtil.getGameName(gid) + "征收cd中,休眠：" + GameUtil.getRemainTime(lastimposetime) + "\t 后执行");
			TaskManager.addTask(new Impose(gid), GameUtil.convertInt(lastimposetime) / 1000 + 1);
			return;
		}
		// u.setImposeInfo(imposeInfo);
		// 首先查看是否有征收事件
		doImposeAction(imposeInfo);
		// 查看是否有征收次数
		String imposeNumStr = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.imposenum");
		// 查到数据，看看值是多少
		int imposeNum = GameUtil.convertInt(imposeNumStr);
		if (imposeNum > 0)
		{
			// 有征收次数，开始征收吧
			HttpUtil.doGet(gid, GameConstants.IMPOSE_DO_URL, new ImposeDoTaskAction(gid));
		} else
		{
			// 如果征收次数为0，那么查看强征次数是否小于设定的值
			String forceImposeNumStr = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.forceimposecost");
			// 查到数据，看看值是多少
			int forceImposeNum = GameUtil.convertInt(forceImposeNumStr);
			if (forceImposeNum <= 5)
			{
				// 强征次数小于指定值，开始强征吧
				HttpUtil.doGet(gid, GameConstants.IMPOSE_FORCE_DO_URL, new ImposeDoTaskAction(gid));
			}
		}
	}

	/**
	 * 征收事件处理
	 */
	private void doImposeAction(XmlNodeData imposeInfo)
	{
		// 首先判断是否有征收事件
		List<XmlNodeData> larrydtoList = XmlDataUtil.getNodeInfoList(imposeInfo, "results.larrydto");
		if (!larrydtoList.isEmpty())
		{
			// 有征收事件，先做征收事件吧。判断民忠，是否低于90
			String loyaltyStr = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.loyalty");
			int loyalty = GameUtil.convertInt(loyaltyStr);
			if (loyalty < 90)
			{
				// 判断事件名称
				String larrydName = XmlDataUtil.getNodeData(imposeInfo, "results.larrydto.name");
				// 判断事件是否是加金币的
				String opt = GameConstants.imposeEventMap.get(larrydName);
				List<BasicNameValuePair> nvpList = new ArrayList<BasicNameValuePair>();
				nvpList.add(new BasicNameValuePair("opt", opt));
				HttpUtil.doPost(gid, GameConstants.IMPOSE_SELECT_URL, nvpList, new ImposeActionSelectTask(gid));
			}

		}
	}

}

/**
 * 征收事件选择
 * 
 * @author Administrator
 * 
 */
class ImposeActionSelectTask extends AbstractXmlActionAfterRequest
{

	public ImposeActionSelectTask(Integer gid)
	{
		super();
		this.gid = gid;
		this.taskUnit = new Impose(gid);
	}

	@Override
	public void doCall(List<Header> headerList, XmlNodeData imposeInfo)
	{
		TaskManager.addTask(new Impose(gid), GameUtil.randomInt(10));
	}
}

/**
 * 执行征收
 * 
 * @author Administrator
 * 
 */
class ImposeDoTaskAction extends AbstractXmlActionAfterRequest
{

	public ImposeDoTaskAction(Integer gid)
	{
		super();
		this.gid = gid;
		this.taskUnit = new Impose(gid);
	}

	public void doCall(List<Header> headerList, XmlNodeData imposeInfo)
	{
		if (state.equals("0"))
		{
			String message = GameUtil.getResultMessage(imposeInfo);
			LoggerUtil.logger(gid, message);
			if (!message.equals("银币数量超过了上限"))
			{
				// 超限量了
				TaskManager.addTask(taskUnit, 1800);
			}
		} else
		{
			UserData u = GameUserInfoUtil.getUser(gid);
			String cooperdis = XmlDataUtil.getNodeData(imposeInfo, "results.cooperdis");
			String golddis = XmlDataUtil.getNodeData(imposeInfo, "results.golddis");
			LoggerUtil.info(gid, GameUserInfoUtil.getGameName(gid) + "征收获得银币：" + cooperdis + "\t 金币：" + golddis);

			// 判断是否还征收次数，如果有，那么提交一个新的，在征收cd结束后开始的征收任务
			String imposenum = XmlDataUtil.getNodeData(imposeInfo, "results.imposenum");
			if (GameUtil.convertInt(imposenum) > 0)
			{
				String imposecd = XmlDataUtil.getNodeData(imposeInfo, "results.imposecd");
				// 提交征收任务，计算下次征收时间
				TaskManager.addTask(new Impose(gid), GameUtil.convertInt(imposecd) / 1000);
			}
		}
	}
}
